const crypto = require('crypto');
/**
 * @param {string} algorithm
 * @param {any} content
 *  @return {string}
 */
const encrypt = (algorithm, content) => {
    let prefix = "hbbq"
    let hash = crypto.createHash(algorithm)
    hash.update(prefix + content)
    return hash.digest('hex')
}
/**
 * @param {any} content
 *  @return {string}
 */
const sha1 = (content) => encrypt('sha1', content)
/**
 * @param {any} content
 *  @return {string}
 */
const md5 = (content) => encrypt('md5', content)

const sha256 = (content) => encrypt('sha256', content)
/**
 * AES 同步对称加密
 * 192位的
 * e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
 * @param password
 * @param content
 * @return {any}
 */
const aesSyncEncode = (password, content) => {
    const algorithm = 'aes-192-cbc'
    const key = crypto.scryptSync(password, 'salt', 24);
    const iv = Buffer.alloc(16, 0); // Initialization vector.
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update(content, 'utf8', 'hex');
    encrypted += cipher.final('hex');
    return encrypted
}

/**
 * AES 解密
 * @param password
 * @param encodeContent
 * @returns {*}
 */
const aesSyncDecode = (password, encodeContent) => {
    const algorithm = 'aes-192-cbc'
    const key = crypto.scryptSync(password, 'salt', 24);
    const iv = Buffer.alloc(16, 0); // Initialization vector.
    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(encodeContent, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted
}
module.exports = {
    sha256, sha1, md5, aesSyncEncode, aesSyncDecode, encrypt
}
